package cn.iocoder.yudao.module.cmt.service.team;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.module.cmt.controller.admin.team.vo.*;
import cn.iocoder.yudao.module.cmt.dal.dataobject.team.TeamApplyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.cmt.dal.mysql.team.TeamApplyMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.cmt.enums.ErrorCodeConstants.*;

@Service
@Validated
public class TeamApplyServiceImpl implements TeamApplyService {

    @Resource
    private TeamApplyMapper teamApplyMapper;

    @Override
    public Long createTeamApply(TeamApplySaveReqVO createReqVO) {
        TeamApplyDO teamApply = BeanUtils.toBean(createReqVO, TeamApplyDO.class);
        teamApplyMapper.insert(teamApply);
        return teamApply.getId();
    }

    @Override
    public void updateTeamApply(TeamApplySaveReqVO updateReqVO) {
        validateTeamApplyExists(updateReqVO.getId());
        TeamApplyDO updateObj = BeanUtils.toBean(updateReqVO, TeamApplyDO.class);
        teamApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteTeamApply(Long id) {
        validateTeamApplyExists(id);
        teamApplyMapper.deleteById(id);
    }

    private void validateTeamApplyExists(Long id) {
        if (teamApplyMapper.selectById(id) == null) {
            throw exception(TEAM_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public TeamApplyDO getTeamApply(Long id) {
        return teamApplyMapper.selectById(id);
    }

    @Override
    public PageResult<TeamApplyDO> getTeamApplyPage(TeamApplyPageReqVO pageReqVO) {
        return teamApplyMapper.selectPage(pageReqVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTeamApplyListByIds(List<Long> ids) {
        ids.forEach(this::validateTeamApplyExists);
        teamApplyMapper.deleteBatchIds(ids);
    }

}
